package edu.sctu.zhengsenwen.month201506.day20150619.Practice;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;

/**
 * Created by user15 on 2015/6/19.
 */
public class InterfaceDemoMammalFrame extends JFrame {

    public static void main(String[] args) {

        InterfaceDemoMammalFrame interfaceDemoMammalFrame = new InterfaceDemoMammalFrame();
        interfaceDemoMammalFrame.setVisible(true);
    }

    JPanel contentPane;

    //声明六个文本编辑器
    JLabel jLabel1 = new JLabel();
    JLabel jLabel2 = new JLabel();
    JLabel jLabel3 = new JLabel();
    JLabel jLabel4 = new JLabel();
    JLabel jLabel5 = new JLabel();
    JLabel jLabel6 = new JLabel();


    //声明六个编辑器
    JTextField jTextFiled1 = new JTextField();
    JTextField jTextFiled2 = new JTextField();
    JTextField jTextFiled3 = new JTextField();
    JTextField jTextFiled4 = new JTextField();
    JTextField jTextFiled5 = new JTextField();
    JTextField jTextFiled6 = new JTextField();

    //声明两个复选框
    JCheckBox jCheckBox1 = new JCheckBox();
    JCheckBox jCheckBox2 = new JCheckBox();


    //声明四个按钮
    JButton jButton1 = new JButton();
    JButton jButton2 = new JButton();
    JButton jButton3 = new JButton();
    JButton jButton4 = new JButton();

    Dog dog;
    Man man;

    //创建接口数组和抽象类数组
    InterfaceSpeak[] soundList = new InterfaceSpeak[2];
    AbstractAnimal[] mammalList = new AbstractAnimal[2];


    public InterfaceDemoMammalFrame() {

        try {
            jbInit();
        } catch (Exception e) {
        }
    }

    private void jbInit() {

        contentPane = (JPanel) this.getContentPane();


        //设置窗口的布局大小及标题

        contentPane.setLayout(null);
        this.setSize(360, 367);
        this.setTitle("综合实例窗口");

        //设置狗类的各个控件属性

        jLabel1.setText("名字：");
        jLabel2.setText("眼睛颜色：");
        jLabel3.setText("年龄：");
        jCheckBox1.setText("可爱");
        jButton1.setText("创建狗");
        jButton1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                jButton1_actionPerformed(e);
            }


        });


        //设置人类的各个控件的属性
        jLabel4.setText("姓名：");
        jLabel5.setText("眼睛颜色：");
        jLabel6.setText("年龄：");
        jCheckBox2.setText("男");
        jButton2.setText("创建人");
        jButton2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                jButton2_actionPerformed(e);
            }


        });


        jButton3.setText("行走速度");
        jButton3.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                jButton3_actionPerformed(e);
            }


        });


        jButton4.setText("叫声种类");
        jButton4.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                jButton4_actionPerformed(e);

            }


        });


        //设置控件大小和位置
        jLabel1.setBounds(94, 17, 54, 33);
        jLabel2.setBounds(71, 58, 66, 24);
        jLabel3.setBounds(96, 100, 44, 18);
        jLabel4.setBounds(97, 151, 44, 25);
        jLabel5.setBounds(74, 189, 66, 26);
        jLabel5.setBounds(98, 232, 40, 37);

        jTextFiled1.setBounds(new Rectangle(142, 20, 121, 25));
        jTextFiled2.setBounds(new Rectangle(142, 21, 121, 25));
        jTextFiled3.setBounds(new Rectangle(142, 22, 121, 25));
        jTextFiled4.setBounds(new Rectangle(142, 23, 121, 25));
        jTextFiled5.setBounds(new Rectangle(142, 24, 121, 25));
        jTextFiled6.setBounds(new Rectangle(142, 25, 121, 25));

        jCheckBox1.setBounds(new Rectangle(217, 122, 62, 28));
        jCheckBox2.setBounds(new Rectangle(217, 122, 50, 28));

        jButton1.setBounds(new Rectangle(103, 302, 88, 29));
        jButton2.setBounds(new Rectangle(103, 302, 88, 20));


        //向容器加入各个控件
        contentPane.add(jCheckBox1);
        contentPane.add(jCheckBox2);

        contentPane.add(jLabel1);
        contentPane.add(jLabel2);
        contentPane.add(jLabel3);
        contentPane.add(jLabel4);
        contentPane.add(jLabel5);
        contentPane.add(jLabel6);

        contentPane.add(jTextFiled1);
        contentPane.add(jTextFiled2);
        contentPane.add(jTextFiled3);
        contentPane.add(jTextFiled4);
        contentPane.add(jTextFiled5);
        contentPane.add(jTextFiled6);

        contentPane.add(jButton1);
        contentPane.add(jButton2);
        contentPane.add(jButton3);
        contentPane.add(jButton4);

    }

    //当推出窗口时清空内存
    public void processWindowsEvent(WindowEvent e) {
        super.processWindowEvent(e);
        if (e.getID() == WindowEvent.WINDOW_CLOSING) {
            System.exit(0);
        }
    }


    //读方法创建狗类，并将狗类的各个属性附上控件
    public void jButton1_actionPerformed(ActionEvent e) {

        dog = new Dog();
        jTextFiled1.setText(dog.getName());
        jTextFiled2.setText(dog.getEyeColor());
        jTextFiled3.setText(Integer.toString(dog.getAge()));
        jCheckBox1.setSelected(true);
        mammalList[0] = dog;
        //soundList是虚拟数组，通过接口使对象与虚拟类交流信息
        soundList[0] = dog;
    }


    //读方法创建人类，把人类各个属性赋给各个控件
    public void jButton2_actionPerformed(ActionEvent e) {

        jTextFiled4.setText(man.getName());
        jTextFiled5.setText(man.getEyeColor());
        jTextFiled6.setText(Integer.toString(man.getAge()));
        jCheckBox2.setSelected(true);
        mammalList[1] = man;
        //soundList是虚拟数组，通过接口使对象与虚拟类交流信息
        soundList[1] = man;
    }

    public void jButton3_actionPerformed(ActionEvent e) {

        for (int i = 0; i <= 1; i++) {
            mammalList[i].speed();
        }
    }


    public void jButton4_actionPerformed(ActionEvent e) {

        for (int i = 0; i <= 1; i++) {

            soundList[i].speak();
        }

    }

}
